﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;


/// <summary>
/// Author: toanvt
/// CreatedDate: 12/12/2011
/// Description: Anh sản phẩm provider
/// </summary>
namespace EHTWebsite.Code
{
    public class HinhAnhSanPhamProvider
    {
        /// <summary>
        /// Author: toanvt
        /// CreatedDate: 12/12/2011
        /// Description: Inser, Update ảnh sản phẩm
        /// </summary>
        /// <param name="theHinhAnhSanPham">Đối tượng ảnh sản phẩm</param>
        /// <param name="IsAdd">Trạng thái thêm mới hay không</param>
        public static void IUHinhAnhSanPham(HinhAnhSanPham theHinhAnhSanPham, bool IsAdd)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_IUHinhAnhSanPham";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@HinhAnhID", theHinhAnhSanPham.HinhAnhID));
            command.Parameters.Add(new SqlParameter("@SanPhamID", theHinhAnhSanPham.SanPhamID));
            command.Parameters.Add(new SqlParameter("@HinhAnhUrl", theHinhAnhSanPham.HinhAnhUrl));
            command.Parameters.Add(new SqlParameter("@GhiChu", theHinhAnhSanPham.GhiChu));
            command.Parameters.Add(new SqlParameter("@SapXep", theHinhAnhSanPham.SapXep));
            command.Parameters.Add(new SqlParameter("@IsAdd", IsAdd));

            DataProvider.ExecuteNoneQuery(command);
        }

        /// <summary>
        /// Author: toanvt
        /// CreatedDate: 12/12/2011
        /// Description: Xóa hình ảnh sản phẩm theo mã ảnh
        /// </summary>
        /// <param name="HinhAnhID">Mã hình ảnh</param>
        public static void DeleteHinhAnhSanPhamByHinhAnhID(Int32 hinhAnhID)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_DeleteHinhAnhSanPhamByHinhAnhID";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@HinhAnhID", hinhAnhID));

            DataProvider.ExecuteNoneQuery(command);
        }

        /// <summary>
        /// Author: toanvt
        /// CreatedDate: 12/12/2011
        /// Description: Xóa hình ảnh sản phẩm theo mã sản phẩm
        /// </summary>
        /// <param name="SanPhamID">Mã sản phẩm</param>
        public static void DeleteHinhAnhSanPhamsBySanPhamID(Int32 sanPhamID)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_DeleteHinhAnhSanPhamBySanPhamID";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@SanPhamID", sanPhamID));

            DataProvider.ExecuteNoneQuery(command);
        }

        /// <summary>
        /// Author: toanvt
        /// CreatedDate: 12/12/2011
        /// Description: Lấy sanh sách ảnh sản phẩm theo mã sản phẩm
        /// </summary>
        /// <param name="sanPhamID">Mã sản phẩm</param>
        /// <returns>Danh sách ảnh sản phẩm</returns>
        public static DataSet GetHinhAnhSanPhamsBySanPhamID(Int32 sanPhamID)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_GetHinhAnhSanPhamsBySanPhamID";
            command.Parameters.Add(new SqlParameter("@SanPhamID", sanPhamID));
            command.CommandType = CommandType.StoredProcedure;

            return DataProvider.ExecuteDataSet(command);
        }

        /// <summary>
        /// Author: toanvt
        /// CreatedDate: 12/12/2011
        /// Description: Lấy hình sảnh sản phẩm theo tiêu chí tìm kiếm
        /// </summary>
        /// <param name="hinhAnhID">Mã hình ảnh</param>
        /// <param name="sanPhamID">Mã sản phẩm</param>
        /// <returns>Danh sách ảnh sản phẩm</returns>
        public static DataSet GetHinhAnhSanPhamsByFillter(Int32 hinhAnhID, Int32 sanPhamID)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_GetHinhAnhSanPhamByFillter";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@HinhAnhID", hinhAnhID));
            command.Parameters.Add(new SqlParameter("@SanPhamID", sanPhamID));

            return DataProvider.ExecuteDataSet(command);
        }

        /// <summary>
        /// Author: toanvt
        /// CreatedDate: 12/12/2011
        /// Description: Lấy ảnh sản phẩm theo mã hình ảnh
        /// </summary>
        /// <param name="hinhAnhID">Mã ảnh</param>
        /// <returns>Đối tượng ảnh sản phẩm</returns>
        public static HinhAnhSanPham GetHinhAnhSanPhamByHinhAnhID(Int32 hinhAnhID)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_GetHinhAnhSanPhamByHinhAnhID";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@HinhAnhID", hinhAnhID));

            DataSet ds = DataProvider.ExecuteDataSet(command);
            HinhAnhSanPham theHinhAnhSanPham = null;

            if (ds.Tables[0].Rows.Count > 0)
            {
                theHinhAnhSanPham = new HinhAnhSanPham();
                theHinhAnhSanPham.HinhAnhUrl = ds.Tables[0].Rows[0]["HinhAnhUrl"].ToString();
                theHinhAnhSanPham.GhiChu = ds.Tables[0].Rows[0]["GhiChu"].ToString();
                theHinhAnhSanPham.SanPhamID = Int32.Parse(ds.Tables[0].Rows[0]["SanPhamID"].ToString());
                theHinhAnhSanPham.SapXep = Int32.Parse(ds.Tables[0].Rows[0]["SapXep"].ToString());
                theHinhAnhSanPham.HinhAnhID = Int32.Parse(ds.Tables[0].Rows[0]["HinhAnhID"].ToString());
            }

            return theHinhAnhSanPham;

        }
    }
}